package com.lyf.common.serialize;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
 * @author lyf
 * @since 2021/2/2 16:25
 */
public class SerializeToString extends JsonSerializer<Integer> {

    @Override
    public void serialize(Integer num, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (num == null || num == 0){
            jsonGenerator.writeString("0");
            return;
        }
        if (num > 0){
            jsonGenerator.writeString("+"+String.valueOf(num));
            return;
        }
        jsonGenerator.writeString("-"+String.valueOf(num));
    }
}
